package com.zyy.smartcity1129;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;
import androidx.fragment.app.Fragment;

import com.youth.banner.Banner;

public abstract class BaseFragment extends Fragment {
    View v;
    View b;
    Boolean bottom;
    public ImageView back;
    public TextView title;
    public TextView right;
    public ImageView imgRight;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        b = inflater.inflate(R.layout.fragment_base, container, false);
        v = inflater.inflate(layId(), (ViewGroup) b, true);
        imgRight = (ImageView) b.findViewById(R.id.imgRight);
        back = (ImageView) b.findViewById(R.id.back);
        title = (TextView) b.findViewById(R.id.title);
        right = (TextView) b.findViewById(R.id.right);
        imgRight.setVisibility(View.GONE);
        right.setVisibility(View.GONE);
        if (back()) {
            back.setOnClickListener(v1 -> {
                remove();
            });
        } else {
            back.setVisibility(View.GONE);
        }
        if (title() == null) {
            title.setVisibility(View.GONE);
        } else {
            title.setText(title());
        }
        bottom = bottom();
        if (bottom == null) {
            bottom = true;
            MainActivity.bottom.setVisibility(View.GONE);
        } else if (!bottom) {
            MainActivity.bottom.setVisibility(View.GONE);
        }
        init(v);
        return b;
    }

    public void th(Runnable runnable) {
        new Thread(runnable).start();
    }

    public void ui(Runnable runnable) {
        requireActivity().runOnUiThread(runnable);
    }

    public void add(Fragment fragment) {
        requireActivity().getSupportFragmentManager().beginTransaction().add(R.id.fragment, fragment).setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right)
                .addToBackStack(getTag()).commit();
    }

    public void remove() {
        requireActivity().getSupportFragmentManager().popBackStack();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (bottom) MainActivity.bottom.setVisibility(View.VISIBLE);
    }

    protected abstract int layId();

    protected abstract String title();

    protected abstract boolean back();

    protected abstract Boolean bottom();

    protected abstract void init(View iv);

}